package net.mksat.gan.keysmanager.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import net.mksat.gan.keysmanager.R;
import net.mksat.gan.keysmanager.database.DbAdapter;
import net.mksat.gan.keysmanager.database.DbLoaderFragment;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by sergey on 8/7/14.
 */
public class CampusActivity extends Activity implements DbLoaderFragment.ReturnResult{
    LinearLayout linearLayout;
    public static String campus;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campus_activity);
        linearLayout = (LinearLayout) findViewById(R.id.listCampus);
        Bundle bundle = new Bundle();
        bundle.putInt(DbLoaderFragment.ID_QUERY, DbLoaderFragment.GET_AUDITORIUM); // запрос на курсор из таблици аудиторий
        getFragmentManager().beginTransaction().add(DbLoaderFragment.newInstance(bundle), "dbLoader").commit();
    }

    @Override
    public void getReturnResult(int result, Cursor cursor) {
        Set<String> setCampus = new HashSet<String>();
        while (cursor.moveToNext()) {
            setCampus.add(cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_CAMPUS)));
        }
        for(final String campus: setCampus){
            Button button = (Button) getLayoutInflater().inflate(R.layout.key_button, null).findViewById(R.id.button);
            button.setText(campus);
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            p.weight = 1;
            button.setLayoutParams(p);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CampusActivity.campus = campus;
                    startActivity(new Intent(CampusActivity.this, MainPageActivity.class));
                }
            });
            linearLayout.addView(button);
        }
    }
}